﻿javascript:(function (){

function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

function in_array(elem,arr) {
  var i;
  for (i = 0; i < arr.length; i++) {
    if (arr[i] == elem) {
      return true;
    }
  }
  return false;
}
function Job(id,name,gains,uses){
    this.id=id;
    this.name = name;
	this.gains = gains;
	this.uses = uses;
}
Job.prototype.toString = function(){
	var ret=this.id+': '+this.name;
	if (this.gains != ''){
		ret+=' bringt: '+this.gains;
	}
	if (this.uses != ''){
		ret+=' braucht: '+this.uses;
	}	
	return ret;
}
function Raid(name,items){
	this.name=name;
	this.items=items;
}
function District(city,tab,name,raid,jobs){
	 this.city=city;
	 this.tab=tab;
	 this.name=name;
	 this.raid=raid;
	 this.jobs=jobs;
}
function getParams(url){
  var ret = new Array();
  for(i=0;i<url.length;i++){
	var param_item = url[i].split("=");
	  ret[param_item[0]] = unescape(param_item[1]);
  }
  return ret;
}
var ItemList = new Array();
var RaidItems = new Array();
var out = '';

		var dname =$('#selected-district-name').text();
		var url = $($('a[id^="btn_dojob"]')[0]).attr('href').split("?");
		url1 = url[1].split('&');		
		var myparams  = getParams(url1);		
		var tab=myparams['tab'];
		var city = myparams['xw_city'];
		
		$("div[id^='job-id-']").each(
			function(i,e){				  				  
				  var id=($(e).attr('id')).split('-')[2];
				  var name = $($(e).find("h4")[0]).text().split('(')[0];
				  var gains='';
				  if ($(e).find("div.boss_item_loot_markup img")[0]){
				    gains = $($(e).find("div.boss_item_loot_markup img")[0]).attr('item_id');
				  }
				  var uses='';
				  if ($(e).find("div.boss_item_consumed_markup img")[0]){
					uses = $($(e).find("div.boss_item_consumed_markup img")[0]).attr('item_id');
				  }
				  //console.info(i+': '+id+' '+name+' '+gains+' '+uses);
				  var itm = new  Job(id,name,gains,uses);
				  ItemList.push(itm);				
			}
			);  
			if (document.getElementById('boss-asset-page-cont') ){
				$("div[class='consumable-item']").each(
					function(i,e){		
						RaidItems.push($($(e).find("div.cons-image img")[0]).attr('item_id'));
						
					}					
				);
			}
			if (RaidItems.length > 0){
				var raid = new Raid(dname+' Asset',RaidItems);
			}
			else{
				raid = '';
			}
			var myDistrict = new District(city,tab,dname,raid,ItemList);
         out+='Stadt: '+city+"\nDistrict: "+dname+"\nTab: "+tab+"\n";
		 for(var i=0;i<ItemList.length;i++){
			 out +=ItemList[i]+"\n";
		 }
		// alert(JSON.stringify(myDistrict));
		//alert("Funzt\n"+out);
var the_div = document.getElementById('mw_masthead');
if (document.getElementById('text_logger') == undefined) {
		var the_count=document.createElement("div");
		the_count.id='district_reader';		
		$(the_count).insertBefore(the_div);
		var the_close = document.createElement('a');
		the_close.id='district_reader_close';
		the_close.innerHTML = '';
		the_close.className='close';
		the_close.href = '#';		
		var jca_out='<textarea id="district_reader_content" cols="80" rows="10">';
		jca_out+=JSON.stringify(myDistrict);
	    jca_out+='</textarea>';
		the_count.innerHTML=jca_out;
		$(the_close).insertBefore(the_count);		
} else {
    document.getElementById('text_logger').value = '';
}	
	$(the_close).click(function(){$('#district_reader').remove();$('#district_reader_close').remove();});
	
return;
})();

